<template>
    <div class="checkBoxModal-wrapper">
        <el-checkbox :indeterminate="isIndeterminate" v-model="checkAll" @change="handleCheckAllChange">全选</el-checkbox>
        <div style="margin: 15px 0;"></div>
        <el-checkbox-group v-model="checkedCities" @change="handleCheckedCitiesChange">
            <el-checkbox style="display: block; margin-bottom: 10px;" v-for="city in cities" :label="city.name"
                :key="city.code">{{ city.name }}</el-checkbox>
        </el-checkbox-group>
        <el-col :span="24" style="border-top: 1px solid #cacaca; margin-top: 15px; padding-top: 10px;">
            <el-button type="primary" size="small" @click="sure">确 定</el-button>
            <el-button type="" size="small" @click="cancel">取 消</el-button>
        </el-col>
    </div>
</template>

<script>
const cityOptions = ['上海', '北京', '广州', '深圳'];
export default {
    props: {
        checkboxItems: {
            type: Array,
            require: true,
            default: [
                {
                    name: '选择一',
                    code: '001'
                },
                {
                    name: '选择二',
                    code: '002'
                },
            ]
        }
    },
    components: {

    },
    data () {
        return {
            checkAll: false,
            checkedCities: [],
            cities: this.checkboxItems,
            isIndeterminate: true
        }
    },
    computed: {
        filterItems () {
            return this.checkboxItems.map(el => el['name'])
        }
    },
    watch: {

    },
    created () {

    },
    mounted () {

    },
    methods: {
        handleCheckAllChange (val) {
            this.checkedCities = val ? this.filterItems : [];
            this.isIndeterminate = false;
        },
        handleCheckedCitiesChange (value) {
            let checkedCount = value.length;
            this.checkAll = checkedCount === this.cities.length;
            this.isIndeterminate = checkedCount > 0 && checkedCount < this.cities.length;
        },
        sure () {
            let a = []
            this.checkboxItems.forEach(el => {
                this.checkedCities.forEach(ej => {
                    if (el.name == ej) {
                        a.push(el)
                    }
                })

            })
            this.$emit('sure', a)
        },
        cancel () {
            this.$emit('cancel')
        }
    },
    activated () { },
}
</script>

<style lang='scss' scoped>
.checkBoxModal-wrapper {
    width: 190px;
    position: absolute;
    background: #FFF;
    min-width: 150px;
    border: 1px solid #EBEEF5;
    z-index: 2000;
    color: #606266;
    line-height: 1.4;
    text-align: justify;
    font-size: 14px;
    box-shadow: 0 2px 12px 0 rgb(0, 0, 0, 10%);
    word-break: break-all;
    padding: 12px;
}
</style>
